﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net.Utilities;
using Ext.Net;

namespace Web
{
    public partial class Default : PageHelper
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                if (CurrentUser == null)
                    return;
                SiteMapNode siteNode = SiteMap.RootNode;
                Ext.Net.TreeNode root = this.CreateNode(siteNode);
                TreePanel1.Root.Add(root);
            }
        }
        
        private Ext.Net.TreeNode CreateNode(SiteMapNode siteMapNode)
        {
            Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();

            if (!string.IsNullOrEmpty(siteMapNode.Url))
            {
                treeNode.Href = this.Page.ResolveUrl(siteMapNode.Url);
            }

            treeNode.NodeID = siteMapNode.Key;
            treeNode.Text = siteMapNode.Title;
            treeNode.Qtip = siteMapNode.Description;

            SiteMapNodeCollection children = siteMapNode.ChildNodes;

            if (children != null && children.Count > 0)
            {
                foreach (SiteMapNode mapNode in siteMapNode.ChildNodes)
                {
                    treeNode.Nodes.Add(this.CreateNode(mapNode));
                }
            }

            return treeNode;
        }
    }
}